package com.yiwu.common.domain.entity;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @TableName Users
 */

@EqualsAndHashCode(callSuper = true)
@TableName(value = "Users")
@Data
public class Users extends Model<Users> {

    /**
     *
     */
    @TableId
    private Long id;

    /**
     *
     */
    private String userName;

    /**
     *
     */
    private String phoneNumber;

    /**
     *
     */
    private String password;

    /**
     *
     */
    private String email;

    /**
     *
     */
    private String school;

    /**
     * 0 未知 1 男 2 女
     */
    private Integer gender;

    /**
     *
     */
    private String avatarUrl;

    /**
     *
     */
    private String bio;

    /**
     *
     */
    private LocalDateTime createdAt;

    /**
     *
     */
    private LocalDateTime updatedAt;

    private Long lastOffTime;

    /**
     * 0 false 1 true
     */
    private Integer isDelete;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    public void init(String phone) {
        this.bio = "该用户什么也没留下";
        this.id = IdUtil.getSnowflakeNextId();
        this.avatarUrl = "http://1.94.38.238:9000/alsfhkjghxx/e2d7a060e5be249a74387ce7d32fc2b.png";
        this.gender = 0;
        this.email = "";
        this.userName = "易物yw在学校蹲";
        this.school = "家里蹲学校";
        this.createdAt = LocalDateTime.now();
        this.password = "bg" + IdUtil.getSnowflakeNextIdStr() + "fa";
        this.phoneNumber = phone;
        this.lastOffTime = System.currentTimeMillis();
    }

    public Users insertOrLogin(String phone) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number", phone);
        Users users = super.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(users)) {
            init(phone);
            if (!super.insert()) {
                throw new BusinessException(ResponseCode.LOGIN_ERROR);
            }
            return this;
        }
        return users;
    }


    public void selectById(String id) {
        Users user = super.selectById(id);
        this.id = user.getId();
        userName = user.getUserName();
        phoneNumber = user.getPhoneNumber();
        email = user.getEmail();
        school = user.getSchool();
        gender = user.getGender();
        avatarUrl = user.getAvatarUrl();
        bio = user.getBio();
        lastOffTime = user.getLastOffTime();
    }

    public Users getUserInfo() {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number", this.phoneNumber);
        return super.selectOne(queryWrapper);
    }
}